package com.shopping.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shopping.bean.Admin;
import com.shopping.bean.User;

@WebFilter("/*")
public class AdminLoginFilter implements Filter {

   
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest)request;
		HttpServletResponse resp = (HttpServletResponse) response;
		String[] urlPatterns = {"admin.jsp","admin.do","addGoods.jsp","getGoods.do"
								,"order.jsp","getOrderGoods.do","porduct.jsp","review.jsp"
								,"user.jsp","getReview.do","getUser.do","updataOrderGoods.do"
								,"updateGoods.do","updateReview.do","updateUser.do"};
		String uri = req.getRequestURI();
		boolean result = false;
		
		for (int i = 0; i < urlPatterns.length; i++) {
			if(uri.contains(urlPatterns[i])) {
				result = true;
				break;
			}
		}
		
		if(result) {
			Admin admin = (Admin)req.getSession().getAttribute("adminLogin");
			if(admin != null) {
				chain.doFilter(request, response);
				return;
			}else {
				resp.sendRedirect("adminLogin.jsp");
				return;
			}
		}else {
			chain.doFilter(request, response);
			return;
		}
	}

}
